package java3_31;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * 1:baidu Properties的用法,给出案例
 */
public class Homework2 {
    public static void main(String[] args) {
        // 加载属性文件
        Properties props = new Properties();
        try (FileInputStream fis = new FileInputStream("config.properties")) {
            props.load(fis);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 读取属性值
        String name = props.getProperty("name");
        int age = Integer.parseInt(props.getProperty("age"));
        String gender = props.getProperty("gender");

        System.out.println("姓名：" + name);
        System.out.println("年龄：" + age);
        System.out.println("性别：" + gender);

        // 修改属性值
        props.setProperty("age", "20");

        // 保存属性文件
        try (FileOutputStream fos = new FileOutputStream("config.properties")) {
            props.store(fos, "update age");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
